﻿using System;
using System.Collections.Generic;
using System.Web.Hosting;
using Microsoft.SharePoint;

namespace ServBus.ListForm2013.Common.WFXSNOperator
{

    #region Abstract Class

    public abstract class BaseListInfo
    {
        #region Accessor

        public string ListTitle { get; set; }

        public string UniqueName { get; set; }

        public Guid ListID { get; set; }

        public List<BaseContentTypeInfo> ContentTypeInfoList { get; set; }

        #endregion
    }

    public class XListInfo : BaseListInfo
    {
        public XListInfo()
        {
            ContentTypeInfoList = new List<BaseContentTypeInfo>();
        }

        public void GenerateListInfo(SPList p_ListInfo)
        {
            ListID = p_ListInfo.ID;
            ListTitle = p_ListInfo.Title;
            UniqueName = p_ListInfo.Title.Replace(" ", "_");
            UniqueName = ServBus.ListForm2013.Common.WFXSNOperator.FieldHelper.GetUniquefieldName(UniqueName);
        }

        public void AddContentTypeInfo(SPContentType p_ContentType)
        {
            ContentTypeInfo cTypeInfo = new ContentTypeInfo
            {
                ContentTypeId = p_ContentType.Id,
                ContentTypeName = p_ContentType.Name,
                UniqueName = ServBus.ListForm2013.Common.WFXSNOperator.FieldHelper.GetUniquefieldName((ListTitle + "_" + p_ContentType.Name).Replace(" ", "_"))
            };

            foreach (SPField sField in p_ContentType.Fields)
            {
                if (!sField.Hidden && !sField.ReadOnlyField && !sField.Group.Equals("_Hidden"))
                {
                    XFieldInfo sFieldInfo = FieldInfoFactory.GetFieldInfoDefinition(sField, p_ContentType.Name);
                    if (sFieldInfo != null)
                    {
                        cTypeInfo.FieldInfoList.Add(sFieldInfo);
                        if (sField.ShowInDisplayForm == null)
                        {
                            cTypeInfo.ViewFieldList.Add(sFieldInfo);
                        }
                        else
                        {
                            bool showInDispForm = sField.ShowInDisplayForm ?? false;
                            if (showInDispForm)
                            {
                                cTypeInfo.ViewFieldList.Add(sFieldInfo);
                            }
                        }
                    }
                }
            }
            ContentTypeInfoList.Add(cTypeInfo);
        }

        public void AddListFieldReadOnly(SPList p_ListInfo)
        {
            ContentTypeInfo cTypeInfo = new ContentTypeInfo
            {
                ContentTypeId = SPContentTypeId.Empty,
                ContentTypeName = string.Empty,
                UniqueName = string.Empty
            };

            foreach (SPField sField in p_ListInfo.Fields)
            {
                if (!sField.Hidden && !sField.ReadOnlyField && !sField.Group.Equals("_Hidden"))
                {
                    int listTemplateId = p_ListInfo.BaseTemplate.GetHashCode();
                    if (listTemplateId == 100)
                    {
                        if (sField.InternalName.Equals("Title"))
                        {
                            LabelFieldInfo sFieldInfo_Title = FieldInfoFactory.GetLabelFieldInfoDefinition(sField, string.Empty);
                            cTypeInfo.FieldInfoList.Add(sFieldInfo_Title);

                            if (p_ListInfo.Fields.ContainsField("LinkTitle"))
                            {
                                SPField linkTitleField = p_ListInfo.Fields[SPBuiltInFieldId.LinkTitle];
                                XFieldInfo sFieldInfo = new UrlLabelFieldInfo(linkTitleField, sField.InternalName);
                                cTypeInfo.FieldInfoList.Add(sFieldInfo);
                                cTypeInfo.ViewFieldList.Add(sFieldInfo);
                            }
                            
                        }
                        else if (sField.InternalName.Equals("Attachments"))
                        {
                            XFieldInfo sFieldInfo = FieldInfoFactory.GetFieldInfoDefinition(sField, string.Empty);
                            cTypeInfo.FieldInfoList.Add(sFieldInfo);
                            cTypeInfo.ViewFieldList.Add(sFieldInfo);
                        }
                    }
                    else
                    {
                        if (sField.InternalName.Equals("FileLeafRef"))
                        {
                            LabelFieldInfo sFieldInfo_Title = FieldInfoFactory.GetLabelFieldInfoDefinition(sField, string.Empty);
                            cTypeInfo.FieldInfoList.Add(sFieldInfo_Title);
                            if (p_ListInfo.Fields.ContainsField("LinkFilename"))
                            {
                                SPField linkFileNameField = p_ListInfo.Fields[SPBuiltInFieldId.LinkFilename];
                                XFieldInfo sFieldInfo = new UrlLabelFieldInfo(linkFileNameField, sField.InternalName);
                                cTypeInfo.FieldInfoList.Add(sFieldInfo);
                                cTypeInfo.ViewFieldList.Add(sFieldInfo);
                            }
                        }
                            
                    }
                    if (!sField.InternalName.Equals("Title") && !sField.InternalName.Equals("FileLeafRef") && !sField.InternalName.Equals("Attachments"))
                    {
                        LabelFieldInfo sFieldInfo = FieldInfoFactory.GetLabelFieldInfoDefinition(sField,
                            string.Empty);
                        if (sFieldInfo != null)
                        {
                            cTypeInfo.FieldInfoList.Add(sFieldInfo);
                            if (sField.ShowInDisplayForm == null)
                            {
                                cTypeInfo.ViewFieldList.Add(sFieldInfo);
                            }
                            else
                            {
                                bool showInDispForm = sField.ShowInDisplayForm ?? false;
                                if (showInDispForm)
                                {
                                    cTypeInfo.ViewFieldList.Add(sFieldInfo);
                                }
                            }
                        }
                    }
            }
            }
            ContentTypeInfoList.Add(cTypeInfo);
        }
    }

    #endregion
}